package com.xhu.water_supply.service;
import com.xhu.water_supply.dto.OrderAddDTO;
import com.xhu.water_supply.entity.Customer;
import com.xhu.water_supply.entity.Order;
import com.xhu.water_supply.entity.Worker;
import com.xhu.water_supply.mapper.OrderMapper;
import com.xhu.water_supply.mapper.CustomerMapper;
import com.xhu.water_supply.mapper.WorkerMapper;
import com.xhu.water_supply.query.OrderQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private WorkerMapper workerMapper;
    @Autowired
    private CustomerMapper customerMapper;

    public List<Order> selectOrderQuery(OrderQuery orderQuery) {
        Example example = new Example(Order.class);
        example.selectProperties("no", "workId", "date", "customerId", "num");
        example.createCriteria().andLike("workId", "%" + orderQuery.getWorkId() + "%")
                .orLike("customerId", "%" + orderQuery.getCustomerId() + "%")
                .orLike("no", "%" + orderQuery.getNo() + "%");
        List<Order> orderList = orderMapper.selectByExample(example);
        return orderList;
    }

    //    public int SelectIdByName(String name) {
//        return workerMapper.SelectIdByName(name);
//    }
//    public int SearchIdByName(String name) {
//        return customerMapper.SelectIdByName(name);
//    }
    public List<Order> selectAll() {
        return orderMapper.selectAll();
    }

    public int deleteByPrimaryKey(int no) {
        return orderMapper.deleteByPrimaryKey(no);
    }

    public int updateByPrimaryKey(Order order) {
        return orderMapper.updateByPrimaryKey(order);
    }

    public Order selectByPrimaryKey(int no) {
        return orderMapper.selectByPrimaryKey(no);
    }

    public int insert(Order order) {
        return orderMapper.insert(order);
    }
//
//    public String getWorkerName(int id) {
//        return workerMapper.findOne(id).getName();
//    }
//    public String getCustomerName(int id){
//        return   customerMapper.selectCustomerById(id);
//    }

    public List<Integer> selectAllWokerId() {
        List<Integer> WokerIdList = new ArrayList<>();
        List<Worker> workerList = workerMapper.findAll();
        workerList.forEach(woker -> {
            int Id = woker.getId();
            WokerIdList.add(Id);
        });
        return WokerIdList;
    }

    public List<Integer> selectAllCusId() {
        List<Integer> cusIdList = new ArrayList<>();
        List<Customer> cusList = customerMapper.findAll();
        cusList.forEach(woker -> {
            int Id = woker.getCid();
            cusIdList.add(Id);
        });
        return cusIdList;
    }
    public int multiDelete(int orderIds[]){
//        return orderMapper.deleteByIds(orderIds);
        int result=0;
        for(int i=0;i<orderIds.length;i++){
            result+=orderMapper.deleteByPrimaryKey(orderIds[i]);
        }
        return result;
    }
}



